package com.wzq.framework.util;

import java.util.List;

import com.lmax.disruptor.util.Util;
/**
 * 分页对象
 * @author 陈剑飞
 *
 * @param <T>
 */
public class Pager<T> {
	/**
	 * 结果列表
	 */
	private List<T> list;
	/**
	 * 页码
	 */
	private int pageNo=1;
	/**
	 * 每页的大小
	 */
	private int pageSize=20;
	/**
	 * 总行数
	 */
	private int totalCount;
	
	/**
	 * 排序字段
	 */
	private String sort;

	/**
	 * 排序顺序
	 */
	private String order = ASC;
	
	/**
	 * 升序
	 */
	public final static String ASC ="ASC";
	
	/**
	 * 降序
	 */
	public final static String DESC ="DESC";
	
	public Pager(){
		
	}
	
	public Pager(int pageNo,int pageSize){
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}
	
	public Pager(int pageNo,int pageSize,String sort,String order){
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.sort = sort;
		this.order = order;
	}
	
	public List<T> getList() {
		return list;
	}
	public int getPageNo() {
		return pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getBeginIndex()
	{
	    int index = (pageNo-1) * pageSize;
		return index <=0 ? 0 :index;
	}
	
	public String getOrderCondition(){
		if(UtilValidate.isNotEmpty(sort)){
		    String sortTemp = sort;
		    if(!sortTemp.contains("_")){
		        sortTemp = UtilString.camelToUnderLineName(sortTemp);
		    }
			return "order by "+sortTemp+" "+(order!=null?order:"");
		}else{
			return "";
		}
	}
	
	
}
